<template>
	<view class="leaveNav">

		<view style="margin-left:45rpx;font-weight: 800;font-size: 38rpx;">{{titleHead}}
			<view class="leaveLine"></view>
		</view>
		<button v-if="userInfo=='部门经理'&&(titleHead!='报修状况'&&titleHead!='报修信息')&&isDuty!='1'"></button>
		<button v-else-if="!btnFont " class="leaveBtn" @click="submit()">提交</button>
		<button v-else-if="titleHead=='报修状况'||titleHead == '最新报修'||titleHead=='正在处理'||titleHead=='记录列表'" @click="skip()"
			class="leaveBtn">{{btnFont}}</button>
	</view>
</template>
<script>
	import * as imageConversion from 'image-conversion'
	export default {
		name: 'btnCom',
		props: {
			titleHead: String,
			btnFont: String,
			repairDesc: String,
			deviceIds: Array,
			photo: {
				type: String,
				default: ''
			},
			repArr: Array,
			repairId: String,
			dormitoryNo: String,
			classDeptId: String,
			type: String,
			buildingNo: String,
			className: String,
			classId: String,
			bdormitoryId: String,
			dormitoryId: String,
			file: Object

		},
		data() {
			return {
				isDuty: 0,
				userInfo: "",
				deviceIdArr: [],
				SbuildingNo: String,
				SclassName: String,
				SclassId: String,
				SbdormitoryId: String,
				SdormitoryId: '',
				SdormitoryNo: String,
				SclassDeptId: String,
			}
		},
		mounted() {
			var that = this
			// uni.getStorage({
			// 	key: 'userInfo',
			// 	success: (res) => {
			// 		that.userInfo = res.data
			// 	}
			// })
			that.userInfo = uni.getStorageSync("userInfo")
			// uni.getStorage({
			// 	key: 'isDuty',
			// 	success: (res) => {
			// 		that.isDuty = res.data
			// 	}
			// })
			that.isDuty = uni.getStorageSync("isDuty")
			this.$forceUpdate()
		},
		methods: {
			submit() {

				if (this.titleHead == '考勤状况') {
					this.$emit('classAttenceConfirm')
				} else if (this.titleHead == '违纪登记') {
					this.$emit('principleFun')
				} else if (this.titleHead == '报修信息' || this.titleHead == '报修状况') {
					this.$emit('maintenanceFun')
				} else if (this.titleHead == "宿舍卫生") {
					this.$emit("dormitoryFun")
				}

			},


			skip() {
				var userId = uni.getStorageSync("loginId")
				uni.showLoading({
					title: '处理中'
				})
				// console.log(this.file)
				// console.log(new FomData())
				console.log("dormitoryNo", this.dormitoryNo)
				if (this.titleHead == '最新报修' || this.titleHead == '正在处理') {
					this.deviceIdArr = []
					if (this.repArr != null) {
						this.repArr.forEach((item, index) => {
							console.log(item.state, index)
							if (item.state == true) {
								this.deviceIdArr.push(item.deviceId)
							}
						})

					}
					var status = 2
					if (this.titleHead == '最新报修') {
						console.log("111111111111")
						if (this.dormitoryNo) {
							this.$api.maintenance.updateRepairStatus(userId, this.repairId, status, this.deviceIdArr, this
								// this.$api.maintenance.updateRepairStatus(this.repairId, status, this.deviceIdArr, this
								.repairDesc).then(res => {
								uni.showToast({
									title: "提交成功"
								})
								setTimeout(function() {
									uni.redirectTo({
										url: "/pages/maintenance/newestMaintenanceStatus"
									})
								}, 500)

							})
						} else {
							// userId,id,status,deviceIds,repairDesc
							// this.$api.maintenance.updateClassRepair(userId, this.repairId, status, this.deviceIdArr, this
							console.log("执行到这里了")
							this.$api.maintenance.updateClassRepair(this.repairId, status, this.deviceIdArr, this
								.repairDesc).then(res => {
								uni.showToast({
									title: "提交成功"
								})
								setTimeout(function() {
									uni.redirectTo({
										url: "/pages/maintenance/newestMaintenanceStatus"
									})
								}, 500)
							})
						}
					} else {
						//  Repair/updateRepairUpload 完成维修 宿舍 带图片
						// ClassRepair/updateClassRepairUpload 班级 带图片
						status = 3;
						// console.log(this.dormitoryNo,this.photo,"dorid")
						// return;
						if (this.dormitoryNo) {
							if (this.photo != '') {
								// alert(2)
								console.log('this.deviceIdArr', this.deviceIdArr)
								uni.uploadFile({
									name: 'upload',
									filePath: this.photo,
									url: uni.url + 'Repair/updateRepairUpload',
									formData: {
										id: this.repairId,
										"repairDesc": this.repairDesc,
										'bdormitoryId': this.bdormitoryId,
										"status": status,
										// "deviceIds": this.deviceIdArr,
										"userId": userId
									},
									header: {
										Authorization: "Bearer " + uni.getStorageSync("token"),
									},
									success: (res) => {
										setTimeout(function() {
											uni.hideLoading();
										}, 2000);
										uni.showToast({
											title: "提交成功"
										})
										setTimeout(function() {
											uni.redirectTo({
												url: "/pages/maintenance/maintenanceIng"
											})
										}, 500)
									}
								})
							} else {
								console.log("这里")
								this.$api.maintenance.updateRepairStatus(userId, this.repairId, status, this.deviceIdArr,
									this
									.repairDesc).then(res => {
									uni.showToast({
										title: "提交成功"
									})
									setTimeout(function() {
										uni.redirectTo({
											url: "/pages/maintenance/maintenanceIng"
										})
									}, 500)
								})
							}

						} else {
							if (this.photo != '') {
								// alert(1)
								console.log("this.deviceIdArr", this.deviceIdArr)
								uni.uploadFile({
									name: 'upload',
									filePath: this.photo,
									url: uni.url + 'ClassRepair/updateClassRepairUpload',
									formData: {
										id: this.repairId,
										"repairDesc": this.repairDesc,
										'classId': this.classId,
										"status": status,
										// "deviceIds": this.deviceIdArr
									},
									header: {
										Authorization: "Bearer " + uni.getStorageSync("token"),
									},
									success: res => {
										setTimeout(function() {
											uni.hideLoading();
										}, 2000);
										uni.showToast({
											title: "提交成功"
										})
										setTimeout(function() {
											uni.navigateTo({
												url: "/pages/maintenance/maintenanceIng"
											})
										}, 500)
									}
								})

							} else {
								this.$api.maintenance.updateClassRepair(this.repairId, status, this.deviceIdArr, this
									.repairDesc).then(res => {
									uni.showToast({
										title: "提交成功"
									})
									setTimeout(function() {
										uni.navigateTo({
											url: "/pages/maintenance/maintenanceIng"
										})
									}, 500)

								})
							}

						}


					}


				} else if (this.titleHead == '记录列表') {
					uni.hideLoading();
					console.log(this.btnFont, "font")
					if (this.btnFont == '入校登记') {
						uni.navigateTo({
							url: '/pages_duty/dutyCheckWorkAttendance/dutyMorinAttendance'
						})
					} else if (this.btnFont == '午休登记') {
						uni.hideLoading()
						uni.navigateTo({
							url: '/pages_duty/dutyCheckWorkAttendance/dutyMoonAttendanceDetail'
						})
					} else if (this.btnFont == '课间违纪') {
						uni.hideLoading()
						uni.navigateTo({
							url: '/pages_duty/dutyCheckWorkAttendance/dutyAfterClassDetail'
						})
					} else if (this.btnFont == '课堂违纪') {
						uni.hideLoading()
						uni.navigateTo({
							url: '/pages_duty/dutyCheckWorkAttendance/dutyClassDetail'
						})
					} else if (this.btnFont == '晚自习登记') {

						uni.navigateTo({
							url: '/pages_duty/dutyCheckWorkAttendance/attendanceStatus'
						})
					}
				} else {

					uni.hideLoading()
					// console.log(this.$emit("maintenanceStatusFun"))
					// this.$emit('maintenanceStatusFun')
					var that = this

					uni.getStorage({
						key: 'dormitoryId',
						success: (res) => {
							that.SdormitoryId = res.data
						}
					})
					uni.getStorage({
						key: 'buildingNo',
						success: (res) => {
							that.SbuildingNo = res.data
						}
					})
					uni.getStorage({
						key: 'classDeptId',
						success: (res) => {
							that.SclassDeptId = res.data
						}
					})
					if (this.SdormitoryId) {
						uni.getStorage({
							key: 'dormitoryNo',
							success: (res) => {
								that.SdormitoryNo = res.data
							}
						})
						uni.navigateTo({
							url: `/pages/maintenance/maintenanceDetail?dormitoryId=${this.SdormitoryId}&dormitoryNo=${this.SdormitoryNo}&buildingNo=${this.SbuildingNo}&type=报修&classDeptId=${this.SclassDeptId}`,
							success: res => {},
							fail: () => {},
							complete: () => {}
						});
					} else {
						var that = this
						uni.getStorage({
							key: 'classId',
							success: (res) => {
								that.SclassId = res.data
							}
						})
						uni.getStorage({
							key: 'className',
							success: (res) => {
								that.SclassName = res.data
							}
						})

						uni.navigateTo({
							url: `/pages/maintenance/maintenanceDetail?classId=${this.SclassId}&className=${this.SclassName}&buildingNo=${this.buildingNo}&type=报修&classDeptId=${this.$route.query.classDeptId}`,
							success: res => {},
							fail: () => {},
							complete: () => {}
						});
					}
				}

			},
		}
	}
</script>

<style>
	uni-button {}

	.leaveNav {
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: row;
		font-size: 38rpx;
		color: blue;
		width: 100%;
		padding: 50upx
	}

	.leaveBtn {
		background-image: url(https://tj-1305380059.cos.ap-nanjing.myqcloud.com/pic/tubiao-01.png);
		background-size: 100%;
		width: 260rpx;
		height: 80rpx;
		color: white;
		font-size: 32rpx;
		line-height: 80rpx;
		font-weight: 0;
		margin-left: 100rpx;
		background-color: #FFFFFF !important;
		border: 0 !important
	}

	.leaveLine {
		width: 220upx;
		height: 5upx;
		background: rgba(239, 220, 11, 1.0);
		opacity: 0.78;
		position: relative;
		bottom: -10upx;
		left: -30upx;
	}

	.leaveMgm {
		color: #808080;
		font-size: 28upx;
	}

	.leaveCheck {
		margin-left: 60upx;
		margin-bottom: 30upx;

	}
</style>